package com.rongke.damo.medicalrecord.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.biz.service.MedicalRecordManager;
import com.rongke.biz.service.UserManager;
import com.rongke.commons.JsonResp;
import com.rongke.model.MedicalRecords;
import com.rongke.model.PathologyReport;
import com.rongke.model.Radiotherapy;
import com.rongke.model.User;
import com.rongke.service.RadiotherapyService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.util.StringUtil;

/**
 * 放疗记录
 * 
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/damo/radiotherapy")
@Transactional
public class DamoRadiotherapyController {
    private static final Logger  LOGGER = Logger.getLogger(DamoRadiotherapyController.class);

    @Autowired
    private RadiotherapyService  radiotherapyService;

    @Autowired
    private UserManager          userManager;

    @Autowired
    private MedicalRecordManager medicalRecordManager;

    /**
     * @添加放疗记录
     * @param radiotherapy
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResp addRadiotherapy(@RequestBody Map<String, String> jsonMap) {
        LOGGER.debug("addRadiotherapy");

        try {
            String mobileNo = jsonMap.get("phone");
            if (StringUtil.isEmpty(mobileNo)) {
                return JsonResp.fa(null);
            }

            User user = userManager.selectOrAddUser(mobileNo);
            if (null == user) {
                return JsonResp.fa(null);
            }
            String userId = user.getUserId();

            MedicalRecords mrInfo = medicalRecordManager.selectOrAddMrByUserId(userId);
            if (null == mrInfo) {
                return JsonResp.fa(null);
            }
            long mrId = mrInfo.getId();

            Radiotherapy radInfo = new Radiotherapy();
            radInfo.setMedicalRecordId(mrId);
            radInfo.setUserId(userId);
            radInfo.setAmount(jsonMap.get("amount"));
            radInfo.setPicContent(jsonMap.get("picContent"));
            radInfo.setStartTime(jsonMap.get("startTime"));
            radInfo.setEndTime(jsonMap.get("endTime"));
            radInfo.setTimes(Integer.parseInt(jsonMap.get("times")));
            radInfo.setTagInfo(jsonMap.get("tagInfo"));

            radiotherapyService.insert(radInfo);

            JSONObject json = new JSONObject();
            json.put("id", radInfo.getId());
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("addRadiotherapy failed", e);
            return JsonResp.fa(null);
        }
    }

    /**
     * @修改放疗记录
     * @param radiotherapy
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updateRadiotherapy(@RequestBody Radiotherapy radiotherapy) {
        LOGGER.debug("updateRadiotherapy");

        try {
            Radiotherapy radInfo = new Radiotherapy();
            radInfo.setId(radiotherapy.getId());
            radInfo.setGmtModified(new Date());
            radInfo.setAmount(radiotherapy.getAmount());
            radInfo.setPicContent(radiotherapy.getPicContent());
            radInfo.setStartTime(radiotherapy.getStartTime());
            radInfo.setEndTime(radiotherapy.getEndTime());
            radInfo.setTimes(radiotherapy.getTimes());
            radInfo.setTagInfo(radiotherapy.getTagInfo());

            radiotherapyService.updateById(radiotherapy);

            JSONObject json = new JSONObject();
            json.put("id", radiotherapy.getId());
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("updateRadiotherapy failed", e);
            return JsonResp.fa(null);
        }
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public JsonResp delRadiotherapyById(long id) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("delRadiotherapyById, id=" + id);
        }

        try {
            radiotherapyService.deleteById(id);

            JSONObject json = new JSONObject();
            json.put("id", id);
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("delRadiotherapyById failed, id=" + id, e);
            return JsonResp.fa(null);
        }
    }

    /**
     * @根据id查找放疗记录
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectRadiotherapy(Long id) {
        LOGGER.debug("selectRadiotherapy, id=" + id);

        try {
            Radiotherapy radiotherapy = radiotherapyService.selectById(id);
            return JsonResp.ok(radiotherapy);
        } catch (Exception e) {
            LOGGER.error("selectRadiotherapy failed", e);
            return JsonResp.fa(null);
        }
    }

    @RequestMapping(value = "/selectAllByPhone", method = RequestMethod.GET)
    public JsonResp selectList(String userId) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("selectList, userId=" + userId);
        }

        try {
            MedicalRecords mr = medicalRecordManager.selectByUserId(userId);
            if (null == mr) {
                return JsonResp.ok();
            }

            long mrId = mr.getId();

            EntityWrapper<Radiotherapy> ew = new EntityWrapper<Radiotherapy>();
            ew.eq("medical_record_id", mrId);
            ew.orderBy("start_time");
            List<Radiotherapy> result = radiotherapyService.selectList(ew);
            return JsonResp.ok(result);
        } catch (Exception e) {
            LOGGER.error("selectList failed, userId=" + userId, e);
            return JsonResp.fa(null);
        }
    }
}
